package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.api.feign.WorkMicroServiceFeign;
import com.imooc.enums.Sex;
import com.imooc.enums.ShowWhichName;
import com.imooc.enums.UserRole;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.service.UsersService;
import com.imooc.utils.DesensitizationUtil;
import com.imooc.utils.LocalDateUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 风间影月
 * @since 2025-03-21
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

//    @Autowired
//    private UsersMapper usersMapper;  冗余注入，父类中已经注入过了

    @Autowired
    private WorkMicroServiceFeign workMicroServiceFeign;

    @Override
    public Users queryUserByMobile(String mobile) {

        return this.getBaseMapper().selectOne(new QueryWrapper<Users>()
                .eq("mobile", mobile));
    }

//    @Transactional
    @GlobalTransactional
    @Override
    public Users createUser(String mobile) {
        Users user = new Users();
        user.setMobile(mobile);
        user.setNickname("用户" + DesensitizationUtil.commonDisplay(mobile));
        user.setRealName("用户" + DesensitizationUtil.commonDisplay(mobile));
        user.setShowWhichName(ShowWhichName.nickname.type);

        user.setSex(Sex.secret.type);
        user.setFace(""); // 默认头像
        user.setEmail("");

        LocalDate birthday = LocalDateUtils.
                parseLocalDate("2000-01-01", LocalDateUtils.DATE_PATTERN);
        user.setBirthday(birthday);

        user.setCountry("中国");
        user.setProvince("");
        user.setCity("");
        user.setDistrict("");
        user.setDescription("");

        user.setStartWorkDate(LocalDate.now());
        user.setPosition("底层码农");
        user.setRole(UserRole.CANDIDATE.type);
        user.setHrInWhichCompanyId("");

        user.setCreatedTime(LocalDateTime.now());
        user.setUpdatedTime(LocalDateTime.now());

        this.baseMapper.insert(user);

        // 发起远程调用，初始化简历,新增一条空记录
        workMicroServiceFeign.init(user.getId());

        int a = 1 / 0;

        return user;
    }
}
